package org.gdpu.service.impl;

import org.gdpu.base.DaoSupport;
import org.gdpu.model.BloodBank;
import org.gdpu.service.BloodBankService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-11-8
 * Time: 下午3:17
 * To change this template use File | Settings | File Templates.
 */
@Service
@Transactional
public class BloodBankServiceImpl extends DaoSupport<BloodBank> implements BloodBankService {

    @Override
    public List<BloodBank> overdue() {
        List<BloodBank> bloodBanks = new ArrayList<BloodBank>();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("state", 1);
        List<BloodBank> banks = (List<BloodBank>) this.findByFields(BloodBank.class, params);
        for (BloodBank bloodBank : banks) {
            long diff = bloodBank.getOverdue().getTime() - new Date().getTime();
            long days = diff / (1000 * 60 * 60 * 24);
            if (days > 0 && days < 10)
                bloodBanks.add(bloodBank);
        }
        return bloodBanks;
    }

    @Override
    public void review(int[] bloodBankIds, int state) {
        StringBuffer jpql = new StringBuffer();
        for (int i = 0; i < bloodBankIds.length; i++) {
            jpql.append('?').append((i + 2)).append(',');
        }
        jpql.deleteCharAt(jpql.length() - 1);
        Query query = em.createQuery("update BloodBank bloodBank set bloodBank.state = ?1 " +
                "where bloodBank.bloodBankId in (" + jpql.toString() + ")");
        query.setParameter(1, state);
        for (int i = 0; i < bloodBankIds.length; i++) {
            query.setParameter(i + 2, bloodBankIds[i]);
        }
        query.executeUpdate();
    }

    @Override
    public void overdue(int[] bloodBankIds, String[] messages) {
        Query query;
        review(bloodBankIds, 3);
        for (int i = 0; i < bloodBankIds.length; i++) {
            query = em.createQuery("update BloodBank bloodBank set bloodBank.message = '" + messages[i] +
                    "' where bloodBank.bloodBankId = " + bloodBankIds[i]);
            query.executeUpdate();

            //更新血库总量
            BloodBank bloodBank = findBloodBankById(bloodBankIds[i]);
            query = em.createQuery("update BloodSum bloodSum set bloodSum.sum = bloodSum.sum - ?1 where bloodSum.bloodType = ?2");
            query.setParameter(1, bloodBank.getBloodVolume());
            query.setParameter(2, bloodBank.getBloodType().getBloodType());
            query.executeUpdate();
        }
    }

    private BloodBank findBloodBankById(int id) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("bloodBankId", id);
        return (BloodBank) this.findSpecialObject(BloodBank.class, params);
    }

    public List<BloodBank> findBloodByState(String state){
        Query query = em.createQuery("select bb from BloodBank bb where bb.state=" + state);
        return query.getResultList();
    }
}
